﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYElectricSetting;

namespace Model
{
	internal class CableTrayFitingSel
	{
		public static FamilySymbol GetFittingType(Document doc, VerticalCableTrayFittingType verticalCableTrayFittingType, CableTrayType cableTrayType)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.WorkingLocation, "CableTrayFittingSet.xml");
			if (!File.Exists(text))
			{
				return result;
			}
			List<XElement> list = (from ele in XElement.Load(text).Elements()
			where ele.Name == "CableTrayType" && ele.Attribute("Type").Value == cableTrayType.Name
			select ele).ToList<XElement>();
			if (list.Count == 1)
			{
				string text2 = "";
				List<XElement> list2 = list[0].Elements().ToList<XElement>();
				switch (verticalCableTrayFittingType)
				{
				case VerticalCableTrayFittingType.LeftDownSideElbow:
					text2 = list2[0].Value;
					break;
				case VerticalCableTrayFittingType.RightDownSideElbow:
					text2 = list2[1].Value;
					break;
				case VerticalCableTrayFittingType.LeftUpSideElbow:
					text2 = list2[2].Value;
					break;
				case VerticalCableTrayFittingType.RightUpSideElbow:
					text2 = list2[3].Value;
					break;
				case VerticalCableTrayFittingType.VerticalUpTee:
					text2 = list2[4].Value;
					break;
				case VerticalCableTrayFittingType.VerticalDownTee:
					text2 = list2[5].Value;
					break;
				case VerticalCableTrayFittingType.VerticalUpSideTee:
					text2 = list2[6].Value;
					break;
				case VerticalCableTrayFittingType.VerticalDownSideTee:
					text2 = list2[7].Value;
					break;
				case VerticalCableTrayFittingType.VerticalUpCornerTee:
					text2 = list2[8].Value;
					break;
				case VerticalCableTrayFittingType.VerticalDownCornerTee:
					text2 = list2[9].Value;
					break;
				case VerticalCableTrayFittingType.VerticalUpCornerCross:
					text2 = list2[10].Value;
					break;
				}
				if (text2 == "")
				{
					return result;
				}
				IList<Element> familyName = RevitFilter.GetElementsOfType(doc, typeof(Family)).ToElements();
				result = CableTrayFitingSel.GetFitingType(text2, familyName);
			}
			return result;
		}

		private static FamilySymbol GetFitingType(string fittingFamilyName, IList<Element> familyName)
		{
			FamilySymbol result = null;
			if (!string.IsNullOrEmpty(fittingFamilyName))
			{
				string tempFamily = fittingFamilyName.Substring(0, fittingFamilyName.IndexOf(":"));
				string b = fittingFamilyName.Substring(fittingFamilyName.IndexOf(":") + 1);
				List<Element> list = (from fi in familyName
				where fi.Name == tempFamily
				select fi).ToList<Element>();
				if (list.Count > 0)
				{
					foreach (FamilySymbol familySymbol in (list[0] as Family).GetSymbols())
					{
						if (familySymbol.Name == b)
						{
							result = familySymbol;
							break;
						}
					}
				}
			}
			return result;
		}
	}
}
